﻿layui.config({
	base: "/Scripts/"
}).use(['form', 'layer', 'jquery', 'laypage', 'upload', 'flow', 'table'], function () {
	var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery,
		table = layui.table;
	//校区管理列表页面
	var schoolManage = {
		//加载列表
		showList: function () {
			table.render({
				elem: '#tb_SchoolList'
				//, height: 315
			  , url: '/SchoolManage/GetSchoolList' //数据接口
			  , id: 'schoolListTable'
			  , page: true //开启分页
			  , method: 'post'
			  , cols: [[ //表头
				{ field: 'SchoolName', title: '校区名称', width: 200, }
				, { field: 'Leader', title: '校区负责人', width: 100 }
				, { field: 'Mobile', title: '手机号码', width: 120, }
				, { field: 'Phone', title: '联系电话', width: 120, }
				, { field: 'Address', title: '校区地址', width: 250, }
				, { toolbar: '#barDemo', title: '操作', width: 250, }
			  ]]
			});
			//监听工具条
			table.on('tool(majortool)', function (obj) {
				var data = obj.data;
				if (obj.event === 'del') {
					layer.confirm('真的删除行么', function (index) {
						//obj.del();
						schoolManage.deleteList(data);
						layer.close(index);
					});
				} else if (obj.event === 'edit') {
					schoolManage.showAddForm(data);
				} else if (obj.event === 'info') {
					schoolManage.showInfoForm(data);
				}
			});
		},
		//搜索列表信息
		searchList: function () {
			var schoolNameVal = $('#schoolName');
			var leaderNameVal = $('#leaderName');
			//执行重载
			table.reload('schoolListTable', {
				page: {
					curr: 1 //重新从第 1 页开始
				}
			  , where: {
				  schoolName: schoolNameVal.val(),
				  leaderName: leaderNameVal.val()
			  }
			});
		},
		//添加修改页面展示
		showAddForm: function (data) {
			var titleName = "添加校区";
			var url = "/SchoolManage/AddSchool";
			if (data != null) {
				url = "/SchoolManage/EditSchool/" + data.Id;
				titleName = "修改校区";
			}
			var index = layui.layer.open({
			    title: titleName,
				type: 2,
				content: url,
				anim: 1, 
				resize: true, 
				maxmin: true, 
				area: ['800px', '400px'],
				success: function (layero, index) {
				}
			})
		},
		//校区详情
		showInfoForm: function (data) {
			var index = layui.layer.open({
				title: "校区详情",
				type: 2,
				maxmin: true,
				area: ['800px', '400px'],
				content: "/SchoolManage/SchoolInfo/" + data.Id,
				success: function (layero, index) {
				}
			})
			//layui.layer.full(index);
		},
		//删除列表信息
		deleteList: function (data) {
			$.post("/SchoolManage/DeleteSchool", { id: data.Id }, function (result) {
				if (result.ResultStatus == 1) {
					layer.msg(result.Message, function () {
						window.location.reload();
					});
				}
				else {
					layer.msg(result.Message, { icon: 6 });
				}
			});
		},
		//操作返回结果
		resultEditForm: function (FormName, URL) {
			var index = top.layer.msg('数据提交中，请稍候', { icon: 16, time: false, shade: 0.8 });
			$("#" + FormName).ajaxSubmit({
				url: URL,　　　　　　//form提交数据的地址
				type: "post",　　　  //form提交的方式(method:post/get)
				//target:target,　　//服务器返回的响应数据显示在元素(Id)号确定
				// beforeSubmit:function(){},　　//提交前执行的回调函数
				success: function (dataRes) {
					if (dataRes.ResultStatus == 1) {
						top.layer.close(index);
						top.layer.msg(dataRes.Message);
						layer.closeAll("iframe");
						parent.location.reload();
					}
					else {
						top.layer.close(index);
						top.layer.msg(dataRes.Message);
					}
				},//提交成功后执行的回调函数
				dataType: "json",　　　　　　　//服务器返回数据类型
				//clearForm:true,　　　　　　 //提交成功后是否清空表单中的字段值
				//restForm:true,　　　　　　  //提交成功后是否重置表单中的字段值，即恢复到页面加载时的状态
				timeout: 6000 　　　　　 　 //设置请求时间，超过该时间后，自动退出请求，单位(毫秒)。
			});
		}
	};
	//加载页面数据
	schoolManage.showList();

	//查询
	$(".search_btn").click(function () {
		schoolManage.searchList();
	})

	//添加展示页面
	$(".usersAdd_btn").click(function () {
		schoolManage.showAddForm();
	})

	//删除
	$("body").on("click", ".users_del", function () {
		schoolManage.deleteList();
	})
	//详情
	$("body").on("click", ".users_info", function () {
		schoolManage.showInfoForm();
	})

	//添加列表信息
	form.on("submit(addSchool)", function (data) {
		var url = "/SchoolManage/AddSchool";
		if (parseInt($("#Id").val()) > 0) {
			url = "/SchoolManage/EditSchool";
		}
		schoolManage.resultEditForm("addSchoolForm", url)
		return false;
	})

	//返回列表，刷新父页面
	$("body").on("click", ".backlist", function () {
		parent.location.reload();
	})

	var upload = layui.upload; //得到 upload 对象
	//创建一个上传组件
	var uploadInst = upload.render({
		elem: '#pic_upload' //绑定元素
	  , url: '/SchoolManage/UploadImage' //上传接口
	  , multiple: true
	  , exts: "jpg|png|gif|bmp|jpeg"
		, before: function (obj) {
			//提交前的操作
			layer.msg('图片上传中...', {
				icon: 16,
				shade: 0.01,
				time: 0
			})
		}
	  , done: function (res) {  //上传完毕回调     
		  layer.close(layer.msg());//关闭上传提示窗口http://fly.layui.com/jie/12987/
		  $('#pic_list').html('<img src="' + res.data.src + '" alt="' + res.data.title + '" class="layui-upload-img"><input type="hidden" id="pic_hidden" name="Imgs" value="' + res.data.src + '" />')
	  }
	  , error: function () {
		  //请求异常回调
	  }
	});

})




